//maybe 函子 ：
//解决空值的问题

class MayBe {
    static of (value) {
        return new MayBe(value)
    }
    constructor (value) {
        this._value = value
    }
    map(fn) {
        return this.isNothing() ? MayBe.of(null) : MayBe.of(fn(this._value))
    }
    isNothing () {
        return this._value === null || this._value === undefined
    }
}

//正常情况
// let l = MayBe.of('hello world')
//             .map(x => x.toUpperCase())
// console.log(l)

// 传空值
// let l = MayBe.of(undefined)
//             .map(x => x.toUpperCase())
// console.log(l)

//在 MayBe 函子中，可以处理null问题，但是我们很难确认是哪一步产生的空值问题，如
let r = MayBe.of('hello world')
            .map(x => x.toUpperCase())
            .map(x => null)
            .map(x => x.split(' '))
console.log(r);